package cn.progen.dao;

import cn.progen.model.Fileds;
import cn.progen.model.Table;
import cn.progen.util.MapToObj;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;



public class DB {
	private DButil dbutil=new DButil();
	
	public List<Table> getTableList(String dbName){
		String sql="select "
				+ "table_name as name from "
				+ "information_schema.tables"
				+ " where "
				+ "table_schema='dbName'"
				+ " and table_type='base table'";
		sql=sql.replaceAll("dbName",dbName );		
		List<Map<String,Object>> rs=dbutil.querySql(sql);
		List<Table> rsTable=new ArrayList<Table>();
		rs.forEach(item->{
			String name=(String) item.get("name");			
			rsTable.add(getTableByName(dbName,name));
		});
		return 		rsTable;
	}
	
	public Table getTableByName(String dbName,String name){
		String sql="select "
				+ "column_name as name ,data_type as dataType,column_key as columnKey,column_comment as columnComment "
				+ "from information_schema.columns where "
				+ "table_schema='dbName' "
				+ "and table_name='tableName'";
		sql=sql.replaceAll("dbName",dbName);		
		sql=sql.replaceAll("tableName",name );		
		List<Map<String,Object>> rs=dbutil.querySql(sql);
		Table table=new Table();
		table.setName(name);
		List<Fileds> rsList=(List<Fileds>) MapToObj.MapToObj(rs, Fileds.class);
		table.setFileds(rsList);
		return table;
	}
	
	

}
